//
//  DDPictureCollectionView.swift
//  swift微博
//
//  Created by whitcast on 16/7/20.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
//获取图片显示的视图
let PictureCollectionViewCellIdentifier = "PictureCollectionViewCellIdentifier"

class DDPictureCollectionView: UICollectionView {
    //添加按钮的闭包回回调  传出数据源数组
    var addBtnClourseOfView: ((insosArr: [UIImage])->())?
    //构造方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        //间距
        let margin = 10
        
        let wid = (UIScreen.mainScreen().bounds.size.width - CGFloat(4 * margin))/3.0
        
        let heigh = wid
        
        flowLayout.itemSize = CGSize(width: wid, height: heigh)
        
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        //代理设置
        dataSource = self
        //设置
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //数据源
    var infosArr: [UIImage] = [UIImage](){
        didSet{
            //刷新
            self.reloadData()
        }
    }
    
    
    private func setupUI() {
        //注册cell
        self.registerClass(DDPictureCollectionViewCell.self, forCellWithReuseIdentifier: PictureCollectionViewCellIdentifier)
    }
}

extension DDPictureCollectionView: UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        //如果有cell,就显示多一个加号cell
        if infosArr.count > 0 && infosArr.count < 9 {
            
            return infosArr.count + 1
            
        }else{
            //不显示加号cell
            return infosArr.count
        }
        
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureCollectionViewCellIdentifier, forIndexPath: indexPath) as! DDPictureCollectionViewCell
        //取出cell中的按钮
         cell.btnOfCell?.removeFromSuperview()
        
        if indexPath.item < infosArr.count{
            
            cell.deleteBtn.hidden = false
            
            cell.img = infosArr[indexPath.item]
            
            
        }else {
            cell.img = nil
            
            cell.deleteBtn.hidden = true
            //添加按钮的回调
            cell.btnOfCellClourse = {
                //添加按钮到控制器取回调
               self.addBtnClourseOfView?(insosArr: self.infosArr)
            }
        }
        
        cell.deleteClourse = {
            //删除数据
            self.infosArr.removeAtIndex(indexPath.item)
            //刷新
            self.reloadData()
        }
        
        return cell
        
    }
    
    
}

//MARK -- 自定义cell
class DDPictureCollectionViewCell: UICollectionViewCell{
    //添加按钮
    var btnOfCell: UIButton?
    //添加按钮的闭包
    var btnOfCellClourse: (()->())?
    ////MARK:--图片
    var img : UIImage?{
        didSet{
            if img == nil {
                let btn = UIButton()
                btn.setBackgroundImage(UIImage(named: "compose_pic_add"), forState: .Normal)
                btn.setBackgroundImage(UIImage(named: "compose_pic_add_highlighted"), forState: .Highlighted)
                
                contentView.addSubview(btn)
                
                btnOfCell = btn
                //点击事件
                btnOfCell?.addTarget(self, action: "clickToChoosepic", forControlEvents: .TouchUpInside)
                
                btn.snp_makeConstraints(closure: { (make) -> Void in
                    make.edges.equalTo(self).offset(UIEdgeInsetsZero)
                })
                
            }
            imgView.image = img
        }
    }
    ////MARK:--子控件 imageView 大图片
    lazy var imgView: UIImageView = UIImageView()
    ////MARK:--删除按钮
    lazy var deleteBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        //MARK:--删除按钮的点击事件
        btn.addTarget(self, action: "clickToDelete", forControlEvents: .TouchUpInside)
        return btn
    }()
    ////MARK:--删除按钮的点击事件
    @objc private func clickToDelete(){
        //调用闭包
        deleteClourse?()
        
    }
    ////MARK:--删除按钮的闭包
    var deleteClourse:(()->())?
    
    ////MARK:--构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        //初始化UI
        setupUI()
        
    }
    
    //MARK:--添加按钮的点击事件
   @objc private func clickToChoosepic(){
        print("添加按钮点击了")
        btnOfCellClourse?()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ////MARK:--初始化UI
    private func setupUI(){
        //添加
        contentView.addSubview(imgView)
        contentView.addSubview(deleteBtn)
        //约束
        imgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges).offset(UIEdgeInsetsZero)
        }
        deleteBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.trailing.equalTo(self)
        }
        
        
    }
    
    
    
    
    
    
    
}
